﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace Client
{
    public class Reader
    {
        private Client client;
        private NetworkStream stream;
        private StreamReader reader;
        private bool stop = false;
        private string serverData;

        /// <summary>
        /// Creates a Reader object
        /// </summary>
        /// <param name="stream">NetworkStream reference</param>
        /// <param name="client">Client reference</param>
        public Reader(NetworkStream stream ,Client client)
        {
            this.stream = stream;
            this.client = client;
            reader = new StreamReader(stream);
        }

        /// <summary>
        /// Run when starting the Reader
        /// </summary>
        public void RunReader()
        {
            while (!stop)
            {
                try
                {
                    serverData = reader.ReadLine();
                    client.ReaderData(serverData);
                }
                catch
                {
                    stop = true;
                }
            }
            reader.Close();
        }

        /// <summary>
        /// Stops the reader and closes the application
        /// </summary>
        public void CloseApplication()
        {
            stop = true;
        }
    }
}